package com.mdnote.jianzhi_offer.array;

/**
 * @author Rhythm-2019
 * @version 1.0
 * @date 2020/10/11
 * @description 剑指 Offer 04. 二维数组中的查找
 */
public class Offer04 {

    public static void main(String[] args) {
        Offer04 offer04 = new Offer04();

        int[][] matrix = {
                {-1, 3},
        };
        boolean numberIn2DArray = offer04.findNumberIn2DArray(matrix, 3);
        System.out.println(numberIn2DArray);
    }

    public boolean findNumberIn2DArray(int[][] matrix, int target) {

        if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
            return false;
        }
        int i = 0, j = matrix[0].length - 1;

        while (i < matrix.length && j >= 0) {
            if (matrix[i][j] == target) {
                return true;
            } else if (matrix[i][j] > target) {
                j--;
            } else {
                i++;
            }
        }

        return false;
    }
}
